package com.bidevalution.controller.api;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bidevalution.business.service.BeLayoutConfigService;
import com.bidevalution.business.service.BeMethodColumnService;
import com.bidevalution.business.service.BeMethodStepService;
import com.bidevalution.dao.entity.BeLayoutConfig;
import com.bidevalution.dao.entity.BeMethodColumn;
import com.bidevalution.dao.entity.BeMethodStep;
import com.bidevalution.dao.extend.BeMethodDefineExt;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.qtp.core.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/beMethodColumn")
@Api(tags = "环节配置布局数据区域配置")
public class BeMethodColumnController extends BaseController {
	@Autowired
	private BeMethodColumnService beMethodColumnService;
	@Autowired
	private BeMethodStepService beMethodStepService;
	@Autowired
	private BeLayoutConfigService beLayoutConfigService;

	@ApiOperation(value = "表格字段查询", notes = "表格字段查询", response = BeMethodColumn.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "methodStepId", value = "环节ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "layoutCode", value = "布局代码", required = true, dataType = "String", paramType = "query") })
	@GetMapping("/getMethodColumnList")
	public Response<List<BeMethodColumn>> selectList(@RequestParam("methodStepId") String methodStepId,
			@RequestParam("layoutCode") String layoutCode) {
		return Response.ok(beMethodColumnService.selectForList(methodStepId, layoutCode));
	}

	@ApiOperation(value = "查询父节点环节关联字段", notes = "查询父节点环节关联字段", response = BeMethodColumn.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "parentId", value = "父节点ID", required = true, dataType = "String", paramType = "query") })
	@GetMapping("/getParentColumnList")
	public Response<Map<String, List<BeMethodColumn>>> selectParentList(@RequestParam("parentId") String parentId) {
		BeMethodStep beMethodStep = getBeMethodStep(parentId);
		Map<String, List<BeMethodColumn>> columnListMap = Maps.newHashMap();
		if (beMethodStep != null) {
			BeLayoutConfig beLayoutConfig = beLayoutConfigService.selectByPrimaryKey(beMethodStep.getLayoutConfigId());
			if (beLayoutConfig != null && !StringUtils.isEmpty(beLayoutConfig.getLayoutCode())) {
				List<BeMethodColumn> columnList = null;
				for (String layoutCode : beLayoutConfig.getLayoutCode().split(",")) {
					columnList = beMethodColumnService.selectForList(beMethodStep.getSeqId(), layoutCode);
					if (columnList.size() > 0) {
						columnListMap.put(layoutCode, columnList);
					}
				}
			}
		}
		return Response.ok(columnListMap);
	}

	public BeMethodStep getBeMethodStep(String parentId) {
		BeMethodStep beMethodStep = beMethodStepService.selectById(parentId);
		if (beMethodStep == null) {
			return null;
		} else if (!StringUtils.isEmpty(beMethodStep.getLayoutConfigId())) {
			return beMethodStep;
		} else {
			return getBeMethodStep(beMethodStep.getParentId());
		}

	}
}
